import requests
import threading

# 控制并发线程数
MAX_THREADS = 10

headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Origin': 'http://59.215.206.252:1606',
    'Referer': 'http://59.215.206.252:1606/sxglfz/mng/systemsetting/init/gzindex?v=1747359033012',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0',
    'X-Requested-With': 'XMLHttpRequest',
    'Cookie': 'JSESSIONID=E7A00EEC5E40D7D4444F68599109005B',
}

params = {
    'method': 'exportItemAllDetailExcelByRegOrOrg',
}
orgCode=[
"52033020170320130807002386",
"52033020170320130956002387",
"52033020170320131216002388",
"52033020170320131311002390",
"52033020170320131510002391",
"52033020170320131549002392",
"52033020170320131750002393",
"52033020170320131835002394",
"52033020170320131940002395",
"52033020170320132052002396",
"52033020170320132137002397",
"52033020170320132235002398",
"52033020170320132405002401",
"52033020170320132434002402",
"52033020170320132505002403",
"52033020170320132531002404",
"52033020170320132557002405",
"52033020170320132627002406",
"52033020170320132658002407",
"52033020170320132747002408",
"52033020170320132818002409",
"52033020170320132913002410",
"52033020170320132940002411",
"52033020170320133728002452",
"52033020170320135157002487",
"52033020170320143546002770"
]
def process_org_code(code):
    try:
        data = '{"regionCode":"","orgCode":"' + code + '","itemCode":"","itemType":""}'
        response = requests.post(
            'http://59.215.206.252:1606/sxglfz/mng/exprot',
            params=params,
            headers=headers,
            data=data,
            verify=False,
        )
        print(response.text)
    except Exception as e:
        print(f"处理机构代码 {code} 时出错: {str(e)}")

# 创建线程池
threads = []
for code in orgCode:
    while threading.active_count() > MAX_THREADS:
        pass  # 等待线程数降到阈值以下
    
    t = threading.Thread(target=process_org_code, args=(code,))
    t.start()
    threads.append(t)

# 等待所有线程完成
for t in threads:
    t.join()

